<?php
include('./adm_header.php');
?>
			<div id="content">
				<?php
				if (isset($_GET['p']))
				{
					if (empty($_GET['p']))
					{
						echo 'There is no page specified to edit, please return <a href="./index.php">home</a>.';
					}
					else
					{
						// Check for file existance:
						if (file_exists('../pages/subpages/' . $_GET['p']))
						{
							if (isset($_POST['submit']))
							{
								$title = str_replace(array('\\'), array(''), $_POST['title']);
								$body = str_replace(array('\\'), array(''), $_POST['body']);
								// Delete the previous page:
								unlink('../pages/subpages/' . $_GET['p']);
								// Create the new page:
								$fh = fopen('../pages/subpages/' . $_POST['title'], 'w');
								fclose($fh);
								// Write the data to the new page.
								$fh = fopen('../pages/subpages/' . $_POST['title'], 'w');
								fwrite($fh, $_POST['body']);
								fclose($fh);
								echo '<p style="color: green; font-weight: bold;">Page Edited successfully.</p>';
							}
						?>
				<p>When editing a page all HTML is enabled. If the Page Title is the same as another existing page then that page will be overwritten.</p>
				<form action="editsubpage.php?p=<?php echo $_GET['p']; ?>" method="post">
					<strong>Title and Page Order:</strong><br />
					<input type="text" name="title" value="<?php echo $_GET['p']; ?>" style="width: 600px; font-family: Verdana; font-size: 12px;"><br />
					<strong>Page Content:</strong><br />
					<textarea name="body" style="width: 600px; height: 400px; font-family: Verdana; font-size: 12px;"><?php $body = str_replace(array('\\'), array(''), file_get_contents('../pages/subpages/' . $_GET['p'])); echo $body; ?></textarea><br />
					<input type="submit" name="submit" value="Save Changes">
				</form>
						<?php
						}
						else
						{
							echo 'The page specified does not exist, please return <a href="./index.php">home</a>.';
						}
					}
				}
				else
				{
					echo 'There is no page specified to edit, please return <a href="./index.php">home</a>.';
				}
				?>
			</div>
		</div>
	</body>
</html>